package com.ruihu.admin.module.system.user;

import com.ruihu.admin.common.anno.NoValidPrivilege;
import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.constant.SwaggerTagConst;
import com.ruihu.admin.module.system.user.constant.UserResponseCodeConst;
import com.ruihu.admin.module.system.user.domain.bo.UserBO;
import com.ruihu.admin.module.system.user.domain.dto.*;
import com.ruihu.admin.module.system.user.domain.vo.UserVO;
import com.ruihu.admin.module.system.login.domain.RequestTokenBO;
import com.ruihu.admin.util.RequestTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 员工管理
 *
 * @author lidoudou
 * @date 2017年12月19日上午11:34:52
 */
@RestController
@Api(tags = {SwaggerTagConst.Admin.MANAGER_USER})
@OperateLog
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/user/query")
    @ApiOperation(value = "用户管理查询", notes = "用户管理查询 @author lidoudou")
    public ResponseDTO<PageResultDTO<UserVO>> query(@RequestBody UserQueryDTO query) {
       return ResponseDTO.succData(userService.selectUserList(query));
    }

    @GetMapping("/user/info/{userId}")
    @ApiOperation(value = "用户详情", notes = "用户详情 @author lidoudou")
    public ResponseDTO<UserVO> queryByUserId(@PathVariable("userId") Long userId) {
        return ResponseDTO.succData(userService.queryByUserId(userId));
    }

    @GetMapping("/user/get/all")
    @ApiOperation(value = "查询所有员工基本信息，用于选择框", notes = "查询所有员工基本信息，用于选择框")
    @NoValidPrivilege
    public ResponseDTO<List<UserVO>> getAll() {
        return ResponseDTO.succData(userService.getAllUser());
    }

    @GetMapping("/user/getByName/{userName}")
    @ApiOperation(value = "查询所有员工基本信息，用于选择框", notes = "查询所有员工基本信息，用于选择框")
    @NoValidPrivilege
    public ResponseDTO<List<UserVO>> getByName(@PathVariable String userName) {
        return this.userService.getUserListByName(userName);
    }

    @ApiOperation(value = "新增用户", notes = "@author yandanyang")
    @PostMapping("/user/add")
    public ResponseDTO<String> addUser(@Valid @RequestBody UserAddDTO emp) {
        RequestTokenBO requestToken = RequestTokenUtil.getRequestUser();
        return userService.addUser(emp, requestToken);
    }

    @ApiOperation(value = "禁用单个员工", notes = "@author yandanyang")
    @GetMapping("/user/updateStatus/{UserId}/{status}")
    public ResponseDTO<String> updateStatus(@PathVariable("UserId") Long UserId, @PathVariable("status") Integer status) {
        if (null == UserId) {
            return ResponseDTO.wrap(UserResponseCodeConst.USER_ID_NULL);
        }
        userService.updateStatus(UserId, status);
        return ResponseDTO.succ();
    }

    @ApiOperation(value = "批量禁用", notes = "@author yandanyang")
    @PostMapping("/user/batchUpdateStatus")
    public ResponseDTO<String> batchUpdateStatus(@Valid @RequestBody UserBatchUpdateStatusDTO batchUpdateStatusDTO) {
        userService.batchUpdateStatus(batchUpdateStatusDTO);
        return ResponseDTO.succ();
    }

    @ApiOperation(value = "更新员工信息", notes = "@author yandanyang")
    @PostMapping("/user/update")
    public ResponseDTO<String> updateUser(@Valid @RequestBody UserUpdateDTO userUpdateDto) {
        return userService.updateUser(userUpdateDto);
    }

    @ApiOperation(value = "删除用户信息", notes = "@author yandanyang")
    @PostMapping("/user/delete/{UserId}")
    public ResponseDTO<String> deleteUserById(@PathVariable("UserId") Long UserId) {
        return userService.deleteUserById(UserId);
    }

    @ApiOperation(value = "单个用户角色授权", notes = "@author yandanyang")
    @PostMapping("/user/updateRoles")
    public ResponseDTO<String> updateRoles(@Valid @RequestBody UserUpdateRolesDTO updateRolesDTO) {
        userService.updateRoles(updateRolesDTO);
        return ResponseDTO.succ();
    }

    @ApiOperation(value = "修改密码", notes = "@author yandanyang")
    @PostMapping("/user/updatePwd")
    public ResponseDTO<String> updatePwd(@Valid @RequestBody UserUpdatePwdDTO updatePwdDTO) {
        RequestTokenBO requestToken = RequestTokenUtil.getRequestUser();
        return userService.updatePwd(updatePwdDTO, requestToken);
    }

    @ApiOperation(value = "通过部门id获取当前部门的人员&没有部门的人", notes = "@author yandanyang")
    @GetMapping("/user/listUserByDeptId/{deptId}")
    public ResponseDTO<List<UserVO>> listUserByDeptId(@PathVariable Long deptId) {
        return userService.getUserByDeptId(deptId);
    }

    @ApiOperation(value = "用户重置密码", notes = "@author lizongliang")
    @GetMapping("/user/resetPasswd/{userId}")
    public ResponseDTO resetPassword(@PathVariable("userId") Integer userId) {
        return userService.resetPasswd(userId);
    }

}
